﻿var name = $("#name"),
			email = $("#email"),
			password = $("#password"),
			allFields = $([]).add(name).add(email).add(password),
			tips = $(".validateTips");
var AllRoleDB;
var helper = new Helper();
var grid;
var gridRole;
var data = [];
var columns = [
    { id: "ID", name: "ID", field: "mprodh_grpID", width: 120, cssClass: "cell-title" },
    { id: "mprodh_grpname", name: "Product Group Name", field: "mprodh_grpname" },
    { id: "mprodh_displayname", name: "Product Display Name", field: "mprodh_displayname" },
    { id: "mprodh_levelno", name: "Product Hier", field: "mprodh_levelno", width: 80, resizable: false },

//    { id: "isDisable", name: "IsDisable", field: "isDisable", minWidth: 60, formatter: Slick.Formatters.Checkmark, editor: Slick.Editors.Checkbox }
   
  ];
var rolecolumns = [
    { id: "Level", name: "Prod Hier Level", field: "mprodh_levelno" },
    { id: "Name", name: "Name", field: "mprodhlName" },
    { id: "SAP", name: "SAP", field: "mprodhlSAP", width: 100, editor: Slick.Editors.LongText },
    { id: "isDisable", name: "IsDisable", field: "isDisable", minWidth: 60, formatter: Slick.Formatters.Checkmark, editor: Slick.Editors.Checkbox }
    
  ];
var checkboxSelector = new Slick.CheckboxSelectColumn({
    cssClass: "slick-cell-checkboxsel"
});
var checkboxSelectorRole = new Slick.CheckboxSelectColumn({
    cssClass: "slick-cell-checkboxsel"
});
columns.splice(0, 0, checkboxSelector.getColumnDefinition());
rolecolumns.splice(0, 0, checkboxSelectorRole.getColumnDefinition());
var options = {
    editable: true,
    enableRowReordering: true,
    enableCellNavigation: true,
    asyncEditorLoading: false,
    autoEdit: false
};
var optionsRole = {
    editable: true,
    enableRowReordering: true,
    enableCellNavigation: true,
    asyncEditorLoading: false,
    autoEdit: false
};
function RefreshProdGrp(parameters) {
    helper.GetProductCate(function (prodgrp) {
        grid = new Slick.Grid("#UserManagerGrid", prodgrp.AllProduct, columns, options);
        grid.setSelectionModel(new Slick.RowSelectionModel({ selectActiveRow: true }));
        grid.registerPlugin(checkboxSelector);
        grid.onCellChange.subscribe(function (e, args) {
            updateProdGrp(args.item);
        });
    },
    true);
}
function RefreshProdHier(parameters) {
    helper.GetProductHier(function (prodhier) {
        gridRole = new Slick.Grid("#UserRoleGrid", prodhier.AllProdHier, rolecolumns, optionsRole);
        gridRole.setSelectionModel(new Slick.RowSelectionModel({ selectActiveRow: true }));
        gridRole.registerPlugin(checkboxSelectorRole);
        gridRole.onCellChange.subscribe(function (e, args) {
            updateRole(args.item);
        });
    });

}



function updateTips(t) {
    tips
				.text(t)
				.addClass("ui-state-highlight");
    setTimeout(function () {
        tips.removeClass("ui-state-highlight", 1500);
    }, 500);
}

function checkLength(o, n, min, max) {
    if (o.val().length > max || o.val().length < min) {
        o.addClass("ui-state-error");
        updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");
        return false;
    } else {
        return true;
    }
}

function checkLengthSaleGrp(value) {
    if (value.length > 3 || value.length < 0) {
        MsgBox("Length of Sale Group must be between " +
            0 + " and " + 3 + ".");
        return { valid: false, msg: "Length of Sale Group must be between " +
					0 + " and " + 3 + "." };
    } else {
        return { valid: true, msg: null };
    }
}

function checkRegexp(o, regexp, n) {
    if (!(regexp.test(o.val()))) {
        o.addClass("ui-state-error");
        updateTips(n);
        return false;
    } else {
        return true;
    }
}

function deleteUser(userName,func) {

    helper.DeleteProdCate(userName, function (data) {
        RefreshProdGrp();
        MsgBox('Delete ' + data + ' rows success');
    });
}

function deleteRole(role) {

    helper.DeleteProdHier(role, function (data) {
        RefreshProdHier();
        MsgBox('Delete ' + data + ' rows success');
    });
}
function updateRole(RoleData) {
    helper.UpdateRoleDb(RoleData, function (data) {
        MsgBox('Update ' + data + ' rows success');
    });
}

function updateProdGrp(ProdGrpData) {
    helper.UpdateProdGrp(ProdGrpData, function (data) {
        MsgBox('Update ' + data + ' rows success');
    });
}
function FormRoleClear() {
    $('#rolenametxt').val('');
    $('#roleDescripttxt').val('');
}
function FormUserClear() {
    $('#name').val('');
    $('#email').val('');
    $('#password').val('');
    $('#msalegrp').val('');
}
function bindRoleDropdown(parameters) {
    $("#SelectRole >option").remove('');
    helper.GetProductHier(function (data) {
        $.each(data.AllProdHier, function (key, value) {
            if (value.isDisable != true) {
                $("#SelectRole").append($("<option></option>").val(value.mprodh_levelno).html(value.mprodhlName));
            }
        });

    });
}

